package com.onpositive.wiki3.db.impl;

import java.io.File;

import com.onpositive.wiki3.db.catmodel.Categories;
import com.onpositive.wiki3.db.catmodel.Category;
import com.onpositive.wiki3.db.catmodel.ICategoryMatcher;
import com.onpositive.wiki3.db.impl.CountingVocabalary.Pair;

public class Test2 {

	public static void main(String[] args) {
		final WikiDBImpl wikiDBImpl = new WikiDBImpl("C:/work/content");
		try {
			Categories c = new Categories(wikiDBImpl, new File(
					"C:/work/content/n2.cats"), false);

			
			c.removeAllContaining(wikiDBImpl, "_by_quality");
			c.removeAllContaining(wikiDBImpl, "_by_importance");
			int size = c.size();
			c.accept(new ICategoryMatcher() {

				int count;

				@Override
				public int accept(Category st) {
					String string = st.toString();
					if (string.endsWith("_by_type")) {
						st.setTag(1);

						for (Category m : st.getChildCategories()) {
							count++;
							m.setTag(2);
						}
						for (Category m : st.getParentsCategories()) {

							if (string.contains(m.toString())) {
								m.setTag(2);
								count++;
							}
						}
						System.out.println(++count);
					}
					if (string.startsWith("lists_of_")) {
						st.setTag(1);

						// for (Category m : st.getChildCategories()) {
						// count++;
						// m.setTag(2);
						// }
						for (Category m : st.getParentsCategories()) {

							if (string.contains(m.toString())) {
								if (!m.toString().startsWith("lists_of")&&!m.toString().equals("lists")) {
									m.setTag(2);
									count++;
								}
							}
						}
						System.out.println(++count);
					}
					return 0;
				}
			});
			System.out.println(size);
			final CountingVocabalary v=new CountingVocabalary();
			c.accept(new ICategoryMatcher() {
				
				@Override
				public int accept(Category st) {
					String string = st.toString();
					int indexOf = string.indexOf("_by_");
					boolean contains = indexOf!=-1;
					if (contains){
						String substring = string.substring(indexOf+4);
						if (substring.indexOf('_')==-1){
							v.add(substring);
						}
					}
					// TODO Auto-generated method stub
					return 0;
				}
			});
			for (Pair p:v.all()){
				System.out.println(p);
			}
			c.accept(new ICategoryMatcher() {

				@Override
				public int accept(Category st) {
					if (st.getTag() == 0) {
						st.remove();
					}
					return 0;
				}
			});
			System.out.println(c.size());
			c.write(new File("C:/work/content/n7.cats"));
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}
}
